package com.interview.javabasic.JUC;
/*
* 线程之间的通信问题，等待唤醒，生产者消费者模式
* 线程之间交替执行A  B同时操作一个变量。*/
public class A {
    public static void main(String[] args) {
        Data data = new Data();
        new Thread(()->{for (int i=0;i<10;i++){
            try {
                data.increment();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        },"A").start();

    }
}

//单独的线程资源业务、通知等等
class Data{//数字、资源类
    private int number  = 0;
    //+1
    public synchronized void increment() throws InterruptedException {
        if (number!=0){
            this.wait();
        }
        number++;
        System.out.println(Thread.currentThread().getName()+"=>"+number);
        this.notifyAll();
    }

    //-1
    public synchronized void decrement() throws InterruptedException {
        if (number==0){
            this.wait();

        }
        number--;
        System.out.println(Thread.currentThread().getName()+"=>"+number);
        this.notifyAll();

    }

}
